package 狂神多线程;

import java.util.concurrent.locks.ReentrantLock;

public class TestLock {
    public static void main(String[] args) {
        TestLock2 testLock2 = new TestLock2();
        new Thread(testLock2,"杜绍聪").start();
        new Thread(testLock2,"王思聪").start();
        new Thread(testLock2,"无名聪").start();
    }
}




class TestLock2 implements Runnable{

    int tickNums=10;

    private final ReentrantLock lock=new ReentrantLock();



    @Override
    public void run() {
        while(true){

            try{
                lock.lock(); //加锁

                if (tickNums>0){

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                    System.out.println(Thread.currentThread().getName()+tickNums--);

                }else {
                    break;
                }
            }finally {
                lock.lock(); //解锁
            }


        }
    }
}